#######################################################################
#Name of code file: social_media_analysis.R

#Data In: social_media_data.csv

#Data Out: Figure 2a & Figure 2b
######################################################################

#Load Packages

## If you don't have packages installed, uncomment and install
## install.packages(readr)
## install.packages(dplyr)
## install.packages(ggplot2)
## install.packages(lubridate)

library(readr)
library(dplyr)
library(ggplot2)
library(lubridate)

## On the computer used to generate the results of this script, below is what is
## printed to the console when sessionInfo() is run
## sessionInfo()

## > sessionInfo()
## R version 4.1.2 (2021-11-01)
## Platform: aarch64-apple-darwin20 (64-bit)
## Running under: macOS Monterey 12.1

## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/lib/libRlapack.dylib

## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base

## other attached packages:
## [1] lubridate_1.8.0 ggplot2_3.3.5   dplyr_1.0.7     readr_2.1.1

## loaded via a namespace (and not attached):
##  [1] magrittr_2.0.1   hms_1.1.1        tidyselect_1.1.1 munsell_0.5.0
##  [5] colorspace_2.0-2 R6_2.5.1         rlang_0.4.12     fansi_0.5.0
##  [9] grid_4.1.2       gtable_0.3.0     utf8_1.2.2       DBI_1.1.1
## [13] withr_2.4.3      ellipsis_0.3.2   assertthat_0.2.1 tibble_3.1.6
## [17] lifecycle_1.0.1  crayon_1.4.2     purrr_0.3.4      tzdb_0.2.0
## [21] vctrs_0.3.8      glue_1.5.1       compiler_4.1.2   pillar_1.6.4
## [25] generics_0.1.1   scales_1.1.1     pkgconfig_2.0.3

## Uncomment and set working directory to replication folder
## setwd("folder")

#Read in Data
data <- read_csv("data/social_media_data.csv")


###########
#Figure 2a#
###########

#Calculate difference between Triangle and Non-Triangle

triangle_prop<-subset(data, locality=="Triangle")
non_triangle_prop<-subset(data, locality=="Non-Triangle")
dif_prop<-merge(triangle_prop, non_triangle_prop, by="week")
dif_prop$dif<-dif_prop$prop.x-dif_prop$prop.y

#Plot

ggplot(dif_prop, aes(x = week, y = dif, color="#228B22")) +
  geom_smooth(data=subset(dif_prop, pre.x==1), span=2)+
  geom_smooth(data=subset(dif_prop, pre.x==0), span=2)+
  geom_point(shape=15) +
  theme(axis.text.x=element_text(size=10))+
  geom_vline(xintercept = ymd("2019-04-09"), linetype=2, color="grey", size=.6) +
  annotate("text", x = ymd("2019-04-09"), y = .15, label = "Election I", family = "Times", size = 3)+
  geom_vline(xintercept = ymd("2019-09-17"), linetype=2, color="grey", size=.6) +
  annotate("text", x = ymd("2019-09-17"), y = .15, label = "Election II", family = "Times", size = 3)+
  geom_vline(xintercept = ymd("2020-01-26"), linetype=1, color="black", size=.6) +
  annotate("text", x = ymd("2020-01-26"), y = .17, label = "Deal of the Century", family = "Times", size = 3)+
  geom_vline(xintercept = ymd("2020-03-02"), linetype=2, color="grey", size=.6) +
  annotate("text", x = ymd("2020-04-02"), y = .15, label = "Election III",family = "Times", size = 3)+
  scale_color_manual(values = c("#228B22")) +
  labs(color = "",
       x = "Date",
       y = "Weekly Difference in Prop. of Posts")+
  theme(text = element_text(size = 12, family = "Times"),
        legend.key=element_blank(),
        panel.grid.major = element_blank(),
        axis.text.x = element_text(size = 12),
        plot.caption = element_text(size = 12, family = "Times",hjust = -.02),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line = element_line(colour = "black"))+
  scale_color_manual(name = "", labels = c("Triangle vs. \n Non-Triangle"), values = c("#228B22"))
ggsave("plots/figure2a.pdf", width = 10, height = 5)


###########
#Figure 2b#
###########

#Plot

ggplot(data, aes(x = week, y = prop, color = locality)) +
  geom_smooth(data=subset(data, pre==1), span=2)+
  geom_smooth(data=subset(data, pre==0), span=2)+
  geom_point(aes(color=locality, shape=locality)) +
  theme(axis.text.x=element_text(size=10))+
  geom_vline(xintercept = ymd("2019-04-09"), linetype=2, color="grey", size=.6) +
  annotate("text", x = ymd("2019-04-09"), y = .20, label = "Election I", family = "Times", size = 3)+
  geom_vline(xintercept = ymd("2019-09-17"), linetype=2, color="grey", size=.6) +
  annotate("text", x = ymd("2019-09-17"), y = .20, label = "Election II", family = "Times", size = 3)+
  geom_vline(xintercept = ymd("2020-01-26"), linetype=1, color="black", size=.6) +
  annotate("text", x = ymd("2020-01-26"), y = .22, label = "Deal of the Century", family = "Times", size = 3)+
  geom_vline(xintercept = ymd("2020-03-02"), linetype=2, color="grey", size=.6) +
  annotate("text", x = ymd("2020-04-02"), y = .20, label = "Election III",family = "Times", size = 3)+
  scale_color_manual(values = c("dodgerblue3", "firebrick3")) +
  labs(color = "", shape="", size="",
       x = "Date",
       y = "Weekly Prop. of Posts")+
    guides(colour = guide_legend(reverse = T), shape = guide_legend(reverse = TRUE))+
  theme(text = element_text(size = 12, family = "Times"),
        legend.key=element_blank(),
        panel.grid.major = element_blank(),
        axis.text.x = element_text(size = 12),
        plot.caption = element_text(size = 12, family = "Times",hjust = -.02),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line = element_line(colour = "black"))
ggsave("plots/figure2b.pdf", width = 10, height = 5)







